//
//  LLRouteTool.m
//  BillRecords
//
//  Created by admin on 2018/9/20.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "LLRouteTool.h"
#import "TTRecyclingNavigationViewController.h"
#import "TTRecyclingValuationViewController.h"
#import "TTFastBeforeViewController.h"
#import "TTEstimateDetailViewController.h"
#import "TTSpecialViewController.h"
#import "TTSpecialItemViewController.h"

@implementation LLRouteTool

+ (UITabBarController *)getRootViewController
{
    UITabBarController *rootVC = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    return rootVC;
}

+ (UINavigationController *)getCurrentNavigationController
{
    UITabBarController *rootVC = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    // 如果已经有控制器被弹出了，找到最上层的控制器
    UIViewController *topVC = rootVC.presentedViewController;
    while (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    if (topVC) {
        if ([topVC isKindOfClass:[UINavigationController class]]) {
            return (UINavigationController *)topVC;
        } else {
            return nil;
        }
    }
    
    return rootVC.selectedViewController;
}

/**
 去首页
 */
+ (void)gotoHomeViewController
{
    UITabBarController *rootVC = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    // 如果已经有控制器被弹出了，找到最上层的控制器
    UIViewController *topVC = rootVC.presentedViewController;
    while (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    if (topVC) {
        [topVC dismissViewControllerAnimated:YES completion:nil];
    }
    
    [[LLRouteTool getCurrentNavigationController] popToRootViewControllerAnimated:YES];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [rootVC setSelectedIndex:0];
    });
    
}

/**
 去购物车页面
 */
+ (void)gotoShopCartViewController
{
    
    UITabBarController *rootVC = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    // 如果已经有控制器被弹出了，找到最上层的控制器
    UIViewController *topVC = rootVC.presentedViewController;
    while (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    if (topVC) {
        [topVC dismissViewControllerAnimated:YES completion:nil];
    }
    
    [[LLRouteTool getCurrentNavigationController] popToRootViewControllerAnimated:YES];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [rootVC setSelectedIndex:3];
    });

}

/**
 去我的页面
 */
+ (void)gotoMineViewController
{
    UITabBarController *rootVC = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    
    NSInteger index = rootVC.viewControllers.count - 1;
    
    // 如果已经有控制器被弹出了，找到最上层的控制器
    UIViewController *topVC = rootVC.presentedViewController;
    while (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    if (topVC) {
        [topVC dismissViewControllerAnimated:YES completion:nil];
    }
    
    [[LLRouteTool getCurrentNavigationController] popToRootViewControllerAnimated:YES];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [rootVC setSelectedIndex:index];
    });
    
}


// 跳转到某个页面
+ (void)skipViewControllerWIthSkipType:(NSString *)skipType skipTarget:(NSString *)skipTarget
{
    NSInteger type = skipType.integerValue;
    UIViewController *vc;
    
    if (type >= 11101 && type <= 11112) {
        vc = [[CTMediator sharedInstance] goodsList_viewControllerWithTitle:[TTLoginSingle sharedSingle].routeTitleDic[skipType] index:skipTarget.integerValue];
    } else {
        
        switch (type) {
            case 11000:
                vc = [[CTMediator sharedInstance] home_viewController];
                break;
            case 11100:
                vc = [[CTMediator sharedInstance] category_viewController];
                break;
            case 11200:
                vc = [[CTMediator sharedInstance] detail_viewControllerWithStyleID:skipTarget];
                break;
            case 11301:
                vc = [[CTMediator sharedInstance] diamond_viewControllerWithDiamondID:skipTarget];
                break;
            case 11400:
                vc = [[CTMediator sharedInstance] market_viewController];
                break;
            case 11500:
                vc = [[CTMediator sharedInstance] imageDingzhi];
                break;
            case 12000:
                vc = [[CTMediator sharedInstance] keFu_viewController];
                break;
            case 13000:
                vc = [[CTMediator sharedInstance] search_viewControllerWithIndex:skipTarget.integerValue];
                break;
            case 14000:
                vc = [[CTMediator sharedInstance] shopCart_viewController];
                break;
            case 15000:
                vc = [[CTMediator sharedInstance] mine_viewController];
                break;
            case 15100:
                vc = [[CTMediator sharedInstance] managerOrder_viewControllerWithIndex:skipTarget];
                break;
            case 15101:
                vc = [[CTMediator sharedInstance] gotoOrderDetailWithOrderNo:skipTarget];
                break;
            case 15200:
                vc = [[CTMediator sharedInstance] coupon_viewController];
                break;
            case 15300:
                vc = [[CTMediator sharedInstance] collection_viewControllerWithPageType:2];
                break;
            case 15400:
                vc = [[CTMediator sharedInstance] collection_viewControllerWithPageType:1];
                break;
            case 15500:
                vc = [[CTMediator sharedInstance] managerAddress_viewController];
                break;
            case 15800:
                vc = [[CTMediator sharedInstance] set_viewController];
                break;
            case 16000: {
                UIViewController *loginVC = [[CTMediator sharedInstance] login_viewController];
                loginVC.modalPresentationStyle = UIModalPresentationFullScreen;
                [[LLRouteTool getRootViewController] presentViewController:loginVC animated:YES completion:nil];
            }
                break;
            case 16100:
                vc = [[CTMediator sharedInstance] register_viewController];
                break;
            case 17000:
                vc = [[CTMediator sharedInstance] plus_viewController];
                break;
            case 17010:
                vc = [[CTMediator sharedInstance] plusOrder_viewControllerWithIndex:skipTarget];
                break;
            case 17050:
                vc = [[CTMediator sharedInstance] plusSharePosters];
                break;
            case 17060:
                vc = [[CTMediator sharedInstance] plusRenewal];
                break;
            case 18000:
                vc = [[CTMediator sharedInstance] web_viewControllerWithUrlString:skipTarget htmlString:nil];
                break;
            case 19100:
                vc = [[CTMediator sharedInstance] notify_viewController];
                break;
            case 19101:
                vc = [[CTMediator sharedInstance] notifyDetail_viewControllerWithNotifyID:skipTarget];
                break;
            case 19200:
                vc = [[CTMediator sharedInstance] help_viewControlelr];
                break;
            case 19201:
                vc = [[CTMediator sharedInstance] helpDetail_viewControllerWithNotifyID:skipTarget];
                break;
            case 20000:
                vc = [[CTMediator sharedInstance] recycling_viewcontroller];
                break;
            case 20010: {
                vc = [[TTRecyclingNavigationViewController alloc] initWithRootViewController:[[TTRecyclingValuationViewController alloc] init]];
            }
                break;
            case 20011: {
                vc = [[TTRecyclingNavigationViewController alloc] initWithRootViewController:[[TTFastBeforeViewController alloc] init]];
            }
                break;
            case 20012:
                vc = [[CTMediator sharedInstance] recyclingDetail_viewcontrollerWithGoodsID:skipTarget];
                break;
            case 20013: {
                TTEstimateDetailViewController *dvc = [[TTEstimateDetailViewController alloc] init];
                dvc.Reclaim_InformationId = skipTarget;
                vc = [[TTRecyclingNavigationViewController alloc] initWithRootViewController:dvc];
            }
                break;
            case 20014:
                // 发布商品
            break;
            case 21000: {
                vc = [[TTSpecialViewController alloc] init];
            }
                break;
            case 21001: {
                vc = [[TTSpecialItemViewController alloc] initWithActivityId:skipTarget];
            }
                break;
            default:
                break;
        }
    }
    
    if (![TTLoginSingle sharedSingle].isLogin) {
        [vc presentLoginViewController];
        return;
    }
    
    if (vc) {
        if ([vc isKindOfClass:[UINavigationController class]]) {
            vc.modalPresentationStyle = UIModalPresentationFullScreen;
            [[LLRouteTool getCurrentNavigationController] presentViewController:vc
                                                                       animated:YES completion:nil];
        } else {
            [[LLRouteTool getCurrentNavigationController] pushViewController:vc animated:YES];
        }
    }
    
    
}


@end
